﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class ServicePackage
    {
        public ServicePackage() { }

        #region [Get All value from Service Package]
        /// <summary>
        /// Get All value from service package
        /// <para>0     : Get all data</para>
        /// <para>1     : Get hosting</para>
        /// <para>2     : Get domain</para>
        /// </summary>
        /// <param name="type">Type of package: Domain or hosting or all?</param>
        /// <param name="getActive">
        /// <para>0     : false</para>
        /// <para>1     : true</para>
        /// <para>-1    : get all</para>
        /// </param>
        /// <returns>
        /// <para>null      : error</para>
        /// <para>1         : Select data from hosting</para>
        /// <para>2         : select data from domain</para>
        /// </returns>
        public List<PC_ServicePackage_getAllResult> getAllHosting(string type, string getActive)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var result = db.PC_ServicePackage_getAll(type, Int32.Parse(getActive)).ToList();
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Add new Service Package like Linux, window server...]
        /// <summary>
        /// Add a new Service package like Linux, window server or .com, .vn
        /// </summary>
        /// <param name="name">Name package</param>
        /// <param name="description">A short description about package</param>
        /// <param name="currency">The main currency for this package like VND, USD...</param>
        /// <param name="type">Type of package: Domain or Hosting?</param>
        /// <param name="active">Visible/Hidden package</param>
        /// <returns>
        /// <para>0    : Insert successfully</para>
        /// <para>-99  : error</para>
        /// </returns>
        public int add(string name, string description, string currency, string type, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                int isInsert = db.PC_ServicePackage_Ins(name, description, currency, type, active);
                return isInsert;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check insert is valid or not]
        /// <summary>
        /// Check Insert is valid or not
        /// </summary>
        /// <param name="name">Name of package</param>
        /// <param name="description">A short description about package</param>
        /// <param name="currency">The main currency of this package: VND, USD,...</param>
        /// <param name="type">Type of package: Domain or hosting?</param>
        /// <returns>
        ///  <para>1    : successfully</para>
        ///  <para>10   : name is null or empty</para>
        ///  <para>11   : name is greater than 50 characters</para>
        ///  <para>20   : currency is null or empty</para>
        ///  <para>21   : currency is greater than 50 characters</para>
        ///  <para>30   : type is null or empty</para>
        ///  <para>31   : type is greater than 50 characters</para>
        ///  <para>40   : description is greater than 2000 characters</para>
        /// </returns>

        public int addValid(string name, string description, string currency, string type)
        {
            if (String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if (name.Length > 50)
            {
                return 11;
            }
            else if (String.IsNullOrEmpty(currency))
            {
                return 20;
            }
            else if (currency.Length > 50)
            {
                return 21;
            }
            else if (String.IsNullOrEmpty(type))
            {
                return 30;
            }
            else if (type.Length > 50)
            {
                return 31;
            }
            else if (description.Length > 2000)
            {
                return 40;
            }

            return 1;
        }
        #endregion

        #region [Edit service package]
        /// <summary>
        /// Update a new service package
        /// </summary>
        /// <param name="id">Id of service package</param>
        /// <param name="name">Name of service package</param>
        /// <param name="description">A short description about service package</param>
        /// <param name="currency">The main currency for this package</param>
        /// <param name="type">Type of package: Domain or hosting?</param>
        /// <param name="active">Visible/Hiddden the service package</param>
        /// <returns>
        /// 0    : Insert successfully
        /// -99  : error
        /// </returns>
        public int edit(string id, string name, string description, string currency, string type, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                int isEdit = db.PC_ServicePackage_Upd(Int32.Parse(id), name, description, currency, type, active);
                return isEdit;
            }
            catch (Exception ex)
            {
                var a = ex.Message;
                return -99;
            }
        }
        #endregion

        #region [Check edit is valid or not]
        /// <summary>
        /// Check edit is valid or not
        /// </summary>
        /// <param name="id">Id of package</param>
        /// <param name="name">Name of package</param>
        /// <param name="description">A short description about package</param>
        /// <param name="currency">The main currency for this package</param>
        /// <param name="type">Type of package: Domain or hosting?</param>
        /// <returns>
        /// <para>1    : successfully</para>
        /// <para>2    : id is null or empty</para>
        /// <para>3    : id is not valid</para>
        /// <para>10   : name is null or empty</para>
        /// <para>11   : name is greater than 50 characters</para>
        /// <para>20   : currency is null or empty</para>
        /// <para>21   : currency is greater than 50 characters</para>
        /// <para>30   : type is null or empty</para>
        /// <para>31   : type is greater than 50 characters</para>
        /// <para>40   : description is greater than 2000 characters</para>
        /// </returns>
        public int editValid(string id, string name, string description, string currency, string type)
        {
            if (String.IsNullOrEmpty(id))
            {
                return 2;
            }
            
            Regex rId = new Regex("^[0-9]{1,}$");
            Match mId = rId.Match(id);
            if(!mId.Success)
            {
                return 3;
            }
            else if (String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if (name.Length > 50)
            {
                return 11;
            }
            else if (String.IsNullOrEmpty(currency))
            {
                return 20;
            }
            else if (currency.Length > 50)
            {
                return 21;
            }
            else if (String.IsNullOrEmpty(type))
            {
                return 30;
            }
            else if (type.Length > 50)
            {
                return 31;
            }
            else if (description.Length > 2000)
            {
                return 40;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed Service Package]
        /// <summary>
        /// Delete an existed Service Package
        /// </summary>
        /// <param name="id">ID of service package</param>
        /// <returns>
        /// <para>0     : successfully</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int delete(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isDelete = db.PC_ServicePackage_Del(Int32.Parse(id));
                return isDelete;
            }
            catch(Exception ex)
            {
                return -99;
            }
        }
        #endregion
    }
}